﻿
using System;
using System.Threading.Tasks;
using CsharpTool.SignalRServer;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace CsharpTool;

public class HubMessageReceive : IMessageReceive<MsgInfo>
{
    private readonly ILogger<HubMessageReceive> _logger;

    public HubMessageReceive(ILogger<HubMessageReceive> logger)
    {
        _logger = logger;
    }

    public Task ConnectionChangeAsync(ConnectionInfo connectionInfo)
    {
        _logger.LogInformation("SignalR -> S [{id}] {status}", connectionInfo.ConnectionId, connectionInfo.IsConnected ? "已连接" : "已断开");
        return Task.CompletedTask;
    }

    public void Dispose()
    {
        
    }

    public Task ReceiveMsgAsync(string connectionId, MsgInfo msg)
    {
        var data = JsonConvert.SerializeObject(msg, Formatting.Indented);
        _logger.LogInformation("SignalR -> S [{id}] 接收到消息:{rn}{msg}", connectionId, Environment.NewLine, data);
        return Task.CompletedTask;
    }
}
